package com.yunling.qishi.macaca.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jxl.read.biff.BiffException;
/**
 * 用于读取数据驱动所需要的数据
 * @author yunling.he
 *
 */
public class DataProviders {
	//private static final Object string = null;
	private static SimpleTools tools=new SimpleTools();
	
	private DataProviders() {
		
	}
	
	//用于数据驱动数据处理
	//导入excel
	//根据
	
	public static Map<String, Object[][]> getData(String filepath) {
		List<List<String>> readAll=null;
		try {
			readAll = new ExcelUtil(filepath).readAll();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String, Object[][]> map=new HashMap<>();
		List<Object[]> datas=new ArrayList<>();
		Object[] data;
		for (int i=0;i<readAll.size();i++) {
			List<String> list = readAll.get(i);
			
			if (!tools.isRightName(list.get(0)))continue; //判断是否为英文或下划线开头，不是则进行下一条
			if(!map.containsKey(list.get(0))){
				data=new Object[list.size()];
				for (int j=0;j<list.size();j++) {
					data[j]=list.get(j);
				}
				datas.add(data);
				//System.out.println(datas.size());
			}
			//如果当前行不同，则将datas转为Object[][]并加入map中,
			if((i==readAll.size()-1)||((i<readAll.size()-1)&&!(readAll.get(i+1).get(0).equals(list.get(0))))){//最后一行时
				int size = datas.size();
				Object[][] objects=new Object[size][];
				for(int j=0;j<size;j++){
					objects[j]=datas.get(j);
				}
				map.put(list.get(0), objects);
				datas=new ArrayList<>();//重置
			}
		/*	if((i<readAll.size()-1)&&!(readAll.get(i+1).get(0).equals(list.get(0)))){//存在下一行时,且当前行与下一行类名不等时
				int size = datas.size();
				Object[][] objects=new Object[size][];
				for(int j=0;j<size;j++){
					objects[j]=datas.get(j);
				}
				//System.out.println(datas.size());
				map.put(list.get(0), objects);
				datas=new ArrayList<>();//重置
			}*/
		}
		return map;
		
	}
/*public static void main(String[] args) {
		Map<String, Object[][]> data = getData("h:/dataProvider.xls");
		Set<String> keySet = data.keySet();
		for (String string : keySet) {
			System.out.println(string);
			System.out.println(data.get(string).length);
			Object[][] objects = data.get(string);
			for (Object[] objects2 : objects) {
				for (Object object : objects2) {
					System.out.println(object.toString());
				}
				
			}
		}
	}*/
}
